import config from './config.js';
import fun from './function.js';
// import {encryptedData, decryptedData} from '../common/encryp.js'
// import {router,RouterMount} from '../router.js'  //路径换成自己的
// import JSONBIG from 'json-bigint'

let encryp = true  //加密
let that = this

export default {
	request(options = {}) {
		return new Promise((resolve, reject) => {
			let url = options.url;
			
			if (url.indexOf("http://") == -1 && url.indexOf("https://") == -1) {
				options.url = config.domain + url;
			}
			let token = uni.getStorageSync('token');
			// 设置自定义头部content-type
			options.header = {
				'content-type': 'application/json;charset=UTF-8',
				
			}
			if(token){
			  options.header.token =  token
			  
			  // options.header.token = token
			}
			options.complete = (response) => {
				if (response.data.code == -2) {
					uni.removeStorageSync("token")
					uni.removeStorageSync("hasLogin")
					uni.navigateTo({
						url: '/pages/login/login'
					})
				}
				// console.log(response,'response111111')
				if(response.data.code == 503){
					uni.showToast({
						title: response.data.msg,
						icon: "none"
					});
				}
				if (response.statusCode == 200) {
					
					resolve(response.data)
				} else {
					uni.showToast({
						title: '请求异常！',
						icon: "none"
					});
				}
			}
			options.fail = (err)=> {
			        uni.hideLoading();
			        uni.showModal({
			            title: '提示',
			            showCancel: false,
			            content: '当前人数访问过多，请稍后再试'
			        })
			}
			if(options.data && encryp){
			
			  console.log(options.data)

			}
			uni.request(options)
		})
	},

	post(url, data = {}, header = {}) {

		let options = {
			url: url,
			data: data,
			header: header,
			method: "POST"
		}

		return this.request(options);
	},

	get(url, data = {}, header = {}) {
		let options = {
			url: url,
			data: data,
			header: header
		}

		return this.request(options);
	}
};